package com.iotproject.forumservice.controller;


import com.iotproject.commonutils.R;
import com.iotproject.forumservice.entity.vo.AddNotificationVo;
import com.iotproject.forumservice.service.ForumNotificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 消息通知 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2023-04-15
 */
@RestController
@RequestMapping("/forumservice/forum-notification")
public class ForumNotificationController {

    @Autowired
    ForumNotificationService forumNotificationService;

    //评论或回复时，通知接收人(插入记录)
    @PostMapping("/addNotification")
    public R addNotification(@RequestBody AddNotificationVo addNotificationVo) {
        int insert = forumNotificationService.addNotification(addNotificationVo);
        if (insert == 0) {
            return R.error();
        }
        return R.ok();
    }

    //根据userId 查询消息通知(分页)
    /**
     * 根据页码和大小分页查询
     * @param page 当前页，默认为1
     * @param size 当前每页显示行数，默认为5
     * @return 页信息的实体
     */
    @GetMapping("/notificationList/{userId}/{page}/{size}")
    public R getNotificationList(@PathVariable("userId") String userId, @PathVariable("page") Integer page, @PathVariable("size") Integer size) {
        Map<String, Object> maps = forumNotificationService.getNotificationList(userId, page, size);
        return R.ok().data("maps", maps);
    }

    //修改状态为已读
    @PostMapping("updateStatus/{notificationId}")
    public R updateStatus(@PathVariable("notificationId") String notificationId) {
        forumNotificationService.updateStatus(notificationId);
        return R.ok();
    }

}

